//将User对象解构
const { User } = require('../../modle/user');

module.exports = async(req, res, next) => {
    //获取post信息
    const { username, email, role, state, password } = req.body;
    //即将要修改的用户id
    const id = req.query.id;
    //res.send(body.password);

    let user = await User.findOne({_id: id});
    
    if (password == user.password) {
        //密码比对成功
       //res.send('密码比对成功');
       //将用户信息更新到数据库中
       await User.updateOne({_id: id }, {
           username: username,
           email: email,
           role: role,
           state: state
       });

       //修改完成重定向会用户列表
       res.redirect('/admin/user');
        

    }else {
        //密码比对失败
        //res.send('密码比对失败');
        let obj = {path: '/admin/user-edit', message: '密码错误，请填写正确密码',id: id};
        next(JSON.stringify(obj));

    }
    //res.send('ok')
}